package com.mask.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.exception.RollException;
import com.mask.roll.common.util.JwtUtils;
import com.mask.roll.common.util.MD5;
import com.mask.roll.common.util.RollResponse;
import com.mask.ucenter.client.SmsClient;
import com.mask.ucenter.entity.Member;
import com.mask.ucenter.entity.VO.RegisterVO;
import com.mask.ucenter.mapper.MemberMapper;
import com.mask.ucenter.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author hx
 * @since 2021-10-27
 */
@Service
@Slf4j
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private SmsClient smsClient;

    @Override
    public RollResponse login(Member member) {
        String password = MD5.encrypt(member.getPassword());
        String mobile = member.getMobile();

        //判断手机号是否存在
        QueryWrapper<Member> memberWrapper = new QueryWrapper<>();
        memberWrapper.eq("mobile",mobile);
        Member mobileMember = null;
        try {
            mobileMember = baseMapper.selectOne(memberWrapper);
        } catch (Exception e) {
            log.warn("用户{}登录操作:没有该用户:{}",member.getNickname(),e.getMessage());
            throw new RollException(ServiceEnums.UCENTER_MEMBER_NOT_EXIST.getCode(),
                    RollConstants.ERROR,ServiceEnums.UCENTER_MEMBER_NOT_EXIST.getMessage());
        }
        if (!password.equals(mobileMember.getPassword())) {
            log.warn("用户{}登录操作:密码错误",mobileMember.getNickname());
            return RollResponse.result(ServiceEnums.UCENTER_MEMBER_PASSWORD_ERROR.getCode(),
                    RollConstants.ERROR,ServiceEnums.UCENTER_MEMBER_PASSWORD_ERROR.getMessage());
        }
        if (mobileMember.getIsDisabled()) {
            log.warn("用户{}登录操作:该用户已禁用",mobileMember.getNickname());
            return RollResponse.result(ServiceEnums.UCENTER_MEMBER_DISABLED.getCode(),
                    RollConstants.ERROR,ServiceEnums.UCENTER_MEMBER_DISABLED.getMessage());
        }

        //登录成功,生成token
        String token = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        log.info("用户{}登录成功",mobileMember.getNickname());
        return RollResponse.result(ServiceEnums.UCENTER_MEMBER_LOGIN_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.UCENTER_MEMBER_LOGIN_SUCCESS.getMessage(),token);
    }

    @Override
    public RollResponse register(RegisterVO registerVO) {
        String mobile = registerVO.getMobile();
        String code = registerVO.getCode();
        String nickname = registerVO.getNickname();
        String password = registerVO.getPassword();
        //判断注册验证码
        RollResponse response = this.smsClient.checkCode(mobile, code);
        if (response.getLevel().equals(RollConstants.ERROR)){
            return RollResponse.result(response.getCode(),response.getLevel(),response.getMessage());
        }

        //判断手机号是否重复
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            log.warn("用户{}注册操作:手机号已被注册",registerVO.getNickname());
            return RollResponse.result(ServiceEnums.UCENTER_MEMBER_REGISTER_MOBILE_REPEAT.getCode(),
                    RollConstants.WARN,ServiceEnums.UCENTER_MEMBER_REGISTER_MOBILE_REPEAT.getMessage());
        }

        //注册成功
        Member member = new Member();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80");
        baseMapper.insert(member);

        log.info("用户注册成功:{}",member);
        return RollResponse.result(ServiceEnums.UCENTER_MEMBER_REGISTER_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.UCENTER_MEMBER_REGISTER_SUCCESS.getMessage());
    }

    @Override
    public Member getOpenIdMember(String openid) {
        return null;
    }

    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
